home *** CD-ROM | disk | FTP | other *** search
- Path: cph-1.news.DK.net!dkuug!dknet!usenet
- From: cronberg@login.dknet.dk (Michell Cronberg)
- Newsgroups: comp.lang.c++
- Subject: Re: Trees !, Who can help me to improve my trees code ! <janc@pi.net>
- Date: Sun, 21 Jan 1996 14:30:30 -0100
- Organization: DKnet / EUnet Denmark - Login Tjenesten
- Message-ID: <Mmb0na-0FxaC078yn@login.dknet.dk>
- References: <4dr86i$75f@neptunus.pi.net>
- NNTP-Posting-Host: login.dknet.dk
-
- Haven't looked at your code but made my own.
- Mail/post if you have any questions.
-
- Later,
- Michell
-
- Tree.cpp
- --------------------------------------------------------------------------
- // Suggestion for TREE (NOT BALANCED) app.
- // Copenhagen, Jan 1996
- // Michell Cronberg
-
- #include <iostream.h>
- #include <conio.h>
-
- class node // class for nodes in tree
- {
- int value;
- node *left; // left child
- node *right; // right child
-
- public:
- node(int val){value=val; left=NULL; right=NULL;}; // constructor
- void shownode(){cout<<"node = "<<value<<endl;};
- friend tree; // access for tree functions
- };
-
- class tree
- {
- node *first; // pointer to first node in tree
-
- public:
- tree(){first=NULL;}; // constuctor
- node *getfirst(){return first;};
- int add(int val); // add node (!recusive)
- int search(int val, node *tmp); // search for node (!recusive)
- void preordre(node *next); // traversal algorithm (recusive)
- void inordre(node *next); // traversal algorithm (recusive)
- void postordre(node *next); // traversal algorithm (recusive)
- };
-
- int tree::add(int val)
- {
- node *tmp=new node(val); // node added (wild pointer)
-
- if(first==0) // first node in tree
- {
- first=tmp;
- cout<<val<<" as no. 1"<<endl;
- return 1;
- };
-
- node *traverse=first; // tmp pointer used to traverse tree
- node *saveparent=NULL; // tmp pointer to save parent
- int way; // var to save if its left or right
- // child
-
- while(traverse!=NULL)
- {
- saveparent=traverse; // save parent
-
- if(traverse->value>=val) // go left in tree
- {
- traverse=traverse->left;
- way=1;
- }
- else // go right in tree
- {
- traverse=traverse->right;
- way=2;
- }
- };
- // this code hooks the node to the tree as a leave
- if(way==1) saveparent->left=tmp; else saveparent->right=tmp;
-
- cout<<val<<" is added"<<endl;
- return 1;
- };
-
- int tree::search(int val, node *tmp)
- {
- cout<<"Searching for "<<val<<" = ";
- while(tmp!=NULL)
- {
- if(tmp->value==val)
- {
- cout<<"found"<<endl;
- return 1; // node found
-
- }
-
- if(tmp->value>=val) // go left in tree
- tmp=tmp->left;
- else // go right in tree
- tmp=tmp->right;
- };
- cout<<"not found"<<endl;
- return 0; // node not found
- };
-
- void tree::preordre(node *tmp)
- // traverse tree preordre - from leave to top (used ei. to delete tree)
- {
- if(tmp==first) cout<<"Preordre: "<<endl;
- if(tmp!=0)
- {
- tmp->shownode();
- preordre(tmp->left);
- preordre(tmp->right);
- };
- };
-
- void tree::inordre(node *tmp)
- // traverse tree inordre - sort tree
- {
- if(tmp==first) cout<<"Inordre: "<<endl;
- if(tmp!=0)
- {
- inordre(tmp->left);
- tmp->shownode();
- inordre(tmp->right);
- };
- };
-
- void tree::postordre(node *tmp)
- // traverse tree postorder - (top to leave) used ei. to print tree
- {
- if(tmp==first) cout<<"Postordre: "<<endl;
- if(tmp!=0)
- {
- postordre(tmp->left);
- postordre(tmp->right);
- tmp->shownode();
- };
- };
-
- void main()
- {
- clrscr();
- tree newtree;
-
- newtree.add(10);
- newtree.add(5);
- newtree.add(1);
- newtree.add(12);
-
- newtree.preordre(newtree.getfirst());
- newtree.inordre(newtree.getfirst());
- newtree.postordre(newtree.getfirst());
-
- newtree.search(5,newtree.getfirst());
- newtree.search(13,newtree.getfirst());
- newtree.search(10,newtree.getfirst());
-
- };
-
-